package edu.mse.camel.client.posterboard.ui.commands;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;


import com.cea.papyrus.diagraminterchange2.di2.Diagram;

import edu.mse.camel.client.posterboard.posterboard.PBDiagram;
import edu.mse.camel.client.posterboard.posterboard.Posterboard;
import edu.mse.camel.client.posterboard.posterboard.PosterboardFactory;

public class PBUMLDiagramCreateCommand extends AbstractPosterboardCommand {

	private Diagram newDiagram;
	
	/** ShapeDiagram to add to. */
	private final Posterboard parent;
	
	/** The bounds of the new Shape. */
	private Rectangle bounds;

	private PBDiagram pbDiagram;
	
	/**
	 * Create a command that will add a new Shape to a ShapesDiagram.
	 * @param newDiagram the new Shape that is to be added
	 * @param parent the ShapesDiagram that will hold the new element
	 * @param bounds the bounds of the new shape; the size can be (-1, -1) if not known
	 * @throws IllegalArgumentException if any parameter is null, or the request
	 * 						  does not provide a new Shape instance
	 */
	public PBUMLDiagramCreateCommand(Diagram newDiagram, Posterboard parent, Rectangle bounds) {
		this.newDiagram = newDiagram;
		this.parent = parent;
		this.bounds = bounds;
		setLabel("diagram creation");
	}

	/**
	 * Can execute if all the necessary information has been provided. 
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	public boolean canExecute() {
		return newDiagram != null && parent != null && bounds != null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		newDiagram.setPosition(bounds.getLocation());
		newDiagram.setSize(bounds.getSize());
		redo();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	public void redo() {
		pbDiagram = PosterboardFactory.eINSTANCE.createPBDiagram();
		pbDiagram.setDelegate(newDiagram);
		pbDiagram.setSize(newDiagram.getSize());
		pbDiagram.setLocation(newDiagram.getPosition());
		parent.getDiagrams().add(pbDiagram);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		parent.getDiagrams().remove(pbDiagram);
	}
		

}
